package com.laf.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.Page;
import com.laf.model.Lafadmin;
import com.laf.model.Notice;
import com.laf.service.AdminService;
import com.laf.service.NoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: he changjie on 2019/3/17
 * @Description:
 */
@Controller
@RequestMapping("/bg")
public class AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    private NoticeService noticeService;

    @RequestMapping(value = "/index")
    public String index(){
        return "redirect:/views/bk-index.jsp";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Lafadmin admin, HttpSession session){
        Lafadmin lafadmin = adminService.login(admin);
        if(lafadmin!=null && StringUtils.isNotEmpty(lafadmin.getUserName()) && StringUtils.isNotEmpty(lafadmin.getPassword())){
            session.setAttribute("logAdmin",lafadmin);
            return "redirect:/views/bk-index.jsp";
        }else{
            return "forward:/views/bk-login.jsp?userName="+ admin.getUserName()+"&logInfo=error";
        }
    }

    @RequestMapping("/queryAll{type}")
    public String query(@RequestParam(required = false) String cateId,
                        @RequestParam(required = false) String title,
                        @RequestParam(required = false) String lowDate,
                        @RequestParam(required = false) String hiDate ,
                        HttpSession session,
                        @PathVariable Integer type){
        List<Notice> notices = noticeService.queryAll(cateId, title, lowDate, hiDate, type);
        session.setAttribute("cateId",cateId);
        session.setAttribute("title",title);
        session.setAttribute("lowDate",lowDate);
        session.setAttribute("hiDate",hiDate);
        session.setAttribute("Page",notices);
        if(type==0){
            return "redirect:/views/bk-found.jsp";
        }else{
            return "redirect:/views/bk-lost.jsp";
        }
    }

    /**
     * 通过启事编号查找对应启事
     * @param id 启事编号
     * @return 启事
     */
    @RequestMapping(value = "/queryOne/{id}",method = RequestMethod.GET)
    public @ResponseBody Map queryOne(@PathVariable("id") int id){
        Notice notice = noticeService.findById(String.valueOf(id));
        HashMap<String, Object> map = new HashMap<>(2);
        if(notice==null){
            map.put("resultCode",-1);
            map.put("data","");
        }else{
            map.put("resultCode",0);
            map.put("data",notice);
        }
        return map;
    }

    /**
     * 管理员删除一个启事
     * @param id 启事ID
     * @param session session
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable("id") int id, HttpSession session){
        Lafadmin logAdmin = (Lafadmin) session.getAttribute("logAdmin");
        if(logAdmin==null){
            return "{\"status\":\"1\"}";
        }else{
            int delete = noticeService.delete(id);
            if(delete==1){
                return "{\"status\":\"0\"}";
            }else{
                return "{\"status\":\"-1\"}";
            }
        }
    }

    @RequestMapping("/logOut")
    public String logOut(HttpSession session){
        session.invalidate();
        return "redirect:/views/bk-login.jsp";
    }
}
